package com.example.forum.services.impl;

import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void create(ArticleReply articleReply) {
        //  1. 参数校验
        if(articleReply.getArticleId() == null || articleReply.getArticleId() <= 0 ||
                articleReply.getPostUserId() == null  || articleReply.getPostUserId() <= 0 ||
                StringUtil.isEmpty(articleReply.getContent())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        //  2. 封装对象
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);


        //  3. 调用mapper
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString() + "插入数据库失败");
            throw new ApplicationException(ResultCode.FAILED.getMessage());
        }

        //  4. 更新文章 回复数
        //  4.1 校验文章状态
        Article article = articleMapper.selectByPrimaryKey(articleReply.getArticleId());
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
        }
        if(article.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
        }
        //  4.2 组装数据
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);
        //  4.3 更新
        articleMapper.updateByPrimaryKeySelective(updateArticle);

        log.info("回复成功. reply.id = " + articleReply.getId() +
                " reply.articleId = " + articleReply.getArticleId() +
                " reply.postUserId = " + articleReply.getPostUserId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        if(articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        //  校验文章状态
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
        }
        if(article.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
        }
        
        //  调用mapper
        List<ArticleReply> articleReplies = articleReplyMapper.selectRepliesByArticleId(articleId);
        return articleReplies;
    }

    @Override
    public List<ArticleReply> selectByReplyId(Long articleId, Long replyId) {
        if(articleId == null || articleId <= 0 || replyId == null || replyId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }

        //  校验文章状态
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
        }
        if(article.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
        }
        //  校验一楼回复状态
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(replyId);
        if(articleReply == null || articleReply.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_REPLY_NOT_EXISTS.getMessage());
        }

        //  调用mapper
        List<ArticleReply> articleReplies = articleReplyMapper.selectRepliesByReplyId(articleId, replyId);
        return articleReplies;
    }
}
